#include "numIslands.h"

void landDFS(vector<vector<char>>& grid, int r, int c) {
  const int rows = grid.size();
  const int cols = grid[0].size();

  grid[r][c] = '0';
  if (r - 1 >= 0 && grid[r - 1][c] == '1') landDFS(grid, r - 1, c);
  if (r + 1 < rows && grid[r + 1][c] == '1') landDFS(grid, r + 1, c);
  if (c - 1 >= 0 && grid[r][c - 1] == '1') landDFS(grid, r, c - 1);
  if (c + 1 < cols && grid[r][c + 1] == '1') landDFS(grid, r, c + 1);
}

int numIslands(vector<vector<char>>& grid) {
  int rows = grid.size();
  int cols = grid[0].size();

  int nums = 0;
  for (int r = 0; r < rows; r++) {
    for (int c = 0; c < cols; c++) {
      if (grid[r][c] == '1') {
        nums++;
        landDFS(grid, r, c);
      }
    }
  }
  return nums;
}
